﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Diagnostics;

namespace ProjectEuler.Extensions
{
    internal static class ActionExtensions
    {
        public static void Measure(this Action action, out TimeSpan elapsed, out Exception exception)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            try
            {
                action();
                exception = null;
            }
            catch (Exception exc)
            {
                exception = exc;
            }
            finally
            {
                stopwatch.Stop();
                elapsed = stopwatch.Elapsed;
            }
        }

        public static T Measure<T>(this Func<T> func, out TimeSpan elapsed, out Exception exception)
        {
            var result = default(T);

            Measure(new Action(() => result = func()), out elapsed, out exception);

            return result;
        }
    }
}
